#include <vtkActor.h>
#include <vtkCamera.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkPointHandleRepresentation3D.h>
#include <vtkPolyDataMapper.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkArrowSource.h>
#include <vtkCommand.h>
#include <vtkInteractorStyleMultiTouchCamera.h>
#include <vtkTransform.h>
#include <vtkTransformPolyDataFilter.h>

#include "vtktransformwidget.h"

// 回调函数：当手柄位置变化时触发
class HandleCallback : public vtkCommand
{
public:
    static HandleCallback* New()
    {
        return new HandleCallback;
    }

    void Execute(vtkObject* caller, unsigned long eventId, void* callData) override
    {
        if (eventId == vtkCommand::InteractionEvent)
        {
            auto* widget = MyBoxWidget::SafeDownCast(caller);

            if(widget != nullptr)
            {
                vtkTransform * transform = vtkTransform::New();
                widget->GetTransform(transform);
                it->SetUserTransform(transform);
            }
        }
    }

    vtkActor* it = nullptr;
};

int main()
{
    // 创建基础场景
    vtkNew<vtkNamedColors> colors;
    vtkNew<vtkRenderer> renderer;
    vtkNew<vtkRenderWindow> renderWindow;
    renderWindow->AddRenderer(renderer);
    renderWindow->SetWindowName("VTK Handle Widget 示例");

    vtkNew<vtkRenderWindowInteractor> interactor;
    interactor->SetRenderWindow(renderWindow);
    // 设置交互器样式为 vtkInteractorStyleMultiTouchCamera
    vtkSmartPointer<vtkInteractorStyleMultiTouchCamera> style = vtkSmartPointer<vtkInteractorStyleMultiTouchCamera>::New();
    interactor->SetInteractorStyle(style);

    // 创建手柄表示（Representation）
    // vtkNew<vtkTransformRepresentation3D> handleRep;
    // handleRep->GetProperty()->SetColor(colors->GetColor3d("Red").GetData());
    // handleRep->SetHandleSize(30); // 设置手柄大小

    // 设置初始位置（世界坐标系）
    // double initialPosition[3] = {1.0, 0.0, 0.0};
    // handleRep->SetWorldPosition(initialPosition);

    // 创建手柄Widget
    // vtkNew<MyWidget> handleWidget;
    // handleWidget->SetInteractor(interactor);
    // handleWidget->SetRepresentation(handleRep);
    // handleWidget->EnabledOn(); // 启用Widget




    // 添加参考球体（可选）
    vtkNew<vtkArrowSource> arrowSource;


    // 创建变换
    vtkSmartPointer<vtkTransform> transform = vtkSmartPointer<vtkTransform>::New();
    transform->Translate(1.0, 2.0, 3.0); // 设置箭头位置
    transform->RotateZ(45.0); // 设置箭头方向

    // 应用变换
    vtkSmartPointer<vtkTransformPolyDataFilter> transformFilter = vtkSmartPointer<vtkTransformPolyDataFilter>::New();
    transformFilter->SetInputConnection(arrowSource->GetOutputPort());
    transformFilter->SetTransform(transform);

    // sphereSource->SetRadius(1.0);
    vtkNew<vtkPolyDataMapper> sphereMapper;
    sphereMapper->SetInputConnection(transformFilter->GetOutputPort());
    vtkNew<vtkActor> sphereActor;
    // callback->it = sphereActor;
    sphereActor->SetMapper(sphereMapper);
    sphereActor->GetProperty()->SetOpacity(0.3);
    sphereActor->GetProperty()->SetColor(colors->GetColor3d("Gold").GetData());
    renderer->AddActor(sphereActor);

    //添加boxwidget
    vtkNew<MyBoxWidget> boxWidget;
    boxWidget->SetInteractor(renderWindow->GetInteractor());
    boxWidget->SetProp3D(sphereActor);
    boxWidget->PlaceWidget();
    boxWidget->On();
    // boxWidget->OutlineCursorWiresOff();

    // 添加位置变化回调
    vtkNew<HandleCallback> callback;
    callback->it = sphereActor;
    boxWidget->AddObserver(vtkCommand::InteractionEvent, callback);


    // 设置相机和渲染
    renderer->SetBackground(colors->GetColor3d("SlateGray").GetData());
    renderer->ResetCamera();
    renderer->GetActiveCamera()->Azimuth(30);
    renderer->GetActiveCamera()->Elevation(30);
    renderer->ResetCameraClippingRange();

    renderWindow->SetSize(800, 600);
    renderWindow->Render();
    interactor->Start();

    return 0;
}
